<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Crypt;
use Laravel\Passport\HasApiTokens;

class Admins extends Authenticatable
{
    use Notifiable,HasApiTokens;//

    const STATUS_1 = 1;//正常
    const STATUS_2 = 2;//锁定
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--danger">锁定</span>',
    ];
    protected $table = 'admins';
    public $timestamps = false;
    protected $fillable = [
        'username', 'openid','nickname','role_id','login_at','status','avatar','password'
    ];
    protected $hidden = [
        'password'
    ];

    public function role()
    {
        return $this->belongsTo('App\Models\Roles', 'role_id','id');
    }


    protected function register($data){
        if(isset($data['password'])){
            $data['password'] = bcrypt($data['password']);
        }
        $user = $this->create($data);
        if($user === false){
            return false;
        }
        return $user;
    }


    /**
     * passport默认使用email字段作为用户名，如果usename是其他字段必须重新定义findForPassport方法，
     * 且必须是public
     * @param $username
     * @return mixed
     */
    public function findForPassport($username)
    {
        return self::where('openid', $username)->first();
    }


}
